<?php
namespace App\Repositories;

use Carbon\Carbon;
use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

class WithdrawWhitelistRepository
{
    //新增提现白名单
    public function addWithdrawWhitelist($params)
    {
        $userId = app('model_user_data')->where('user_code', $params['user_code'])->value('user_id');
        if (empty($userId)) {
            throw new BasicException(10010, '用户编号不存在');
        }

        $where = [
            'user_id' => $userId,
            'account' => $params['account'],
        ];
        $whitelist = app('model_withdraw_whitelist')->select('id')->where($where)->first();
        if ($whitelist) {
            throw new BasicException(10010, '账户已经添加过白名单了');
        }

        $params['user_id'] = $userId;
        unset($params['user_code']);
        $params['withdraw_total'] = $this->queryUserWithdrawTotal($userId, $params['account']);
        $params['withdraw_total_month'] = $this->queryUserWithdrawTotalMonth($userId, $params['account']);

        return app('model_withdraw_whitelist')->insert($params);
    }

    //查询用户提现总金额
    private function queryUserWithdrawTotal($userId, $alipayAccount)
    {
        return app('model_user_withdraw')
            ->where('user_id', $userId)
            ->where('bank_num', $alipayAccount)
            ->where('status', 2)
            ->sum('money');
    }

    //查询用户本月提现总金额
    private function queryUserWithdrawTotalMonth($userId, $alipayAccount)
    {
        $timestamp_month_start = Carbon::now()->startOfMonth()->timestamp;

        return app('model_user_withdraw')
            ->where('user_id', $userId)
            ->where('bank_num', $alipayAccount)
            ->where('status', 2)
            ->where('add_time', '>=', $timestamp_month_start)
            ->sum('money');
    }

    //获取支付宝账户信息
    public function getAlipayAccountInfo($alipayAccount, $type = 1)
    {
        $data = app('model_user_bank')
            ->select('user_id', 'cardholder as name', 'card_number as id_card_no', 'bank_name', 'bank_zhi')
            ->with('userData:user_id,user_code,nickname')
            ->where('bank_num', $alipayAccount)
            ->where('bank_type', $type)
            ->where('is_deleted', 0)
            ->get()->toArray();
        foreach ($data as &$v) {
            $v = array_merge($v, $v['user_data']);
            unset($v['user_data']);
        }
        if (empty($data)) {
            throw new BasicException(10010, '账户信息不存在');
        }

        return $data;
    }

    //移除提现白名单
    public function delWithdrawWhitelist($ids)
    {
        $id_array = explode(',', $ids);
        if (empty($id_array)) {
            throw new BasicException(0, 'id错误');
        }
        app('model_withdraw_whitelist')->whereIn('id', $id_array)->delete();
    }

    //提现白名单列表
    public function withdrawWhitelist($params)
    {
        $perPage = $params['per_page'] ?? 10;
        $type = $params['type'] ?? 1;
        $where[] = ['a.type', '=', $type];
        if (isset($params['user_code'])) {
            $where[] = ['b.user_code', '=', $params['user_code']];
        }
        if (isset($params['nickname'])) {
            $where[] = ['b.nickname', 'like', $params['nickname'] . "%"];
        }
        if (isset($params['mobile'])) {
            $where[] = ['c.mobile', '=', $params['mobile']];
        }
        if (isset($params['account'])) {
            $where[] = ['a.account', 'like', $params['account'] . "%"];
        }

        return DB::table('ln_withdraw_whitelist as a')
            ->select('a.id', 'a.user_id', 'a.account', 'a.name', 'a.id_card_no', 'a.withdraw_total', 'a.withdraw_total_month', 'a.created_at', 'a.bank_name', 'a.bank_zhi', 'b.nickname', 'b.user_code', 'c.mobile')
            ->leftJoin('ln_user_data as b', 'a.user_id', '=', 'b.user_id')
            ->leftJoin('ln_user as c', 'a.user_id', '=', 'c.user_id')
            ->where($where)
            ->orderByDesc('a.id')
            ->paginate($perPage);
    }
}
